package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.common.R;
import com.example.domain.Category;
import com.example.domain.Dish;
import com.example.domain.Employee;
import com.example.domain.Setmeal;
import com.example.service.CategoryService;
import com.example.service.DishService;
import com.example.service.SetmealService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    /**
     * 分页
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<IPage<Category>> page(@PathParam("page")  Integer page, @PathParam("pageSize") Integer pageSize) {
        IPage<Category> categoryListForPage = categoryService.getCategoryListForPage(page, pageSize);
        R<IPage<Category>> success = R.success(categoryListForPage);
        return success;
    }

    /**
     * 添加套餐
     * @param category
     * @return
     */
    @PostMapping
    public R<String> addCategory(@RequestBody Category  category) {
        if (categoryService.save(category)) {
            return R.success("新增套餐成功");
        }
        return R.error("添加套餐失败,请重新添加");
    }

    /**
     * 编辑套餐回显数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Category> queryCategoryById(@PathVariable Long id){
        try {
            Category category = categoryService.getById(id);
            return R.success(category);
        } catch (Exception exception) {
            exception.printStackTrace();
            return R.error("服务器繁忙");
        }
    }

    /**
     * 更新套餐
     * @param category 需要更新的套餐
     * @return
     */
    @PutMapping
    public R<String> editCategory(@RequestBody Category category){
        try {
            boolean b = categoryService.updateById(category);
            return  R.success("修改成功");
        } catch (Exception exception) {
            exception.printStackTrace();
            return R.error("修改失败");
        }
    }

    /**
     * 删除单个或多个分类
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> deleteCategory( Long id){
        try {
            List<Setmeal> setmeals = setmealService.list(new LambdaQueryWrapper<Setmeal>().eq(Setmeal::getCategoryId, id));
            List<Dish> dishes = dishService.list(new LambdaQueryWrapper<Dish>().eq(Dish::getCategoryId, id));
            if (setmeals.size()>0||dishes.size()>0){
                return R.error("因分类中含菜品或者套餐，导致分类删除失败，请先删除菜品或者套餐");
            }
            boolean b = categoryService.removeById(id);
            return R.success("删除成功");
        } catch (Exception exception) {
            exception.printStackTrace();
            return R.error("删除失败");
        }
    }

    /**
     * 根据分类类型获取所有分类
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> listForType(Category category){
        try {
            List<Category> list = categoryService.list(new LambdaQueryWrapper<Category>()
                    .eq(category.getType()!=null,Category::getType, category.getType()));
            return R.success(list);
        } catch (Exception exception) {
            exception.printStackTrace();
            return R.error("执行失败");
        }
    }
}
